
class Student{
    // 私有属性name
    private String name = "zhangsan";
    //公有属性age
    public int age = 19;
    //不带参数的构造方法
    public Student() {
        System.out.println("Student()");
    }
    // 带参数的构造方法（私有）
    private Student(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("Student(String name, int age)");
    }

    private void eat() {
        System.out.println("我会吃饭");
    }

    public void sleep() {
        System.out.println("我喜欢睡觉");
    }

    private void function(String str) {
        System.out.println(str);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Demo1 {
    public static void main(String[] args) {
        //1.通过 getClass 获取 Class 对象
        Student s1 = new Student();
        Class c1 = s1.getClass();

        //2.通过 类名.class
        Class c2 = Student.class;

        //3.通过 Class 对象的 forName()静态方法获取
        Class c3 = null;
        //可能会抛异常
        try {
            c3 = Class.forName("Student");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        //一个类在 JVM 中只会有一个Class实例
        // 结果都是 true
        System.out.println(c1.equals(c2));
        System.out.println(c1.equals(c3));
        System.out.println(c3.equals(c2));
    }
}
